<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

defined('_JEXEC') or die('Restrisse access');

jimport('joomla.application.component.view');

class ArticleflowViewArticleflow_edit extends JView {

    function display($tpl = null) {

        if ($this->getLayout() == 'form') {
            $this->_displayForm($tpl);
            return;
        }
        //get the chitiet
        //  $duyetbai = & $this->get('data');
        // parent::display($tpl);
    }

    function _displayForm($tpl) {

        global $mainfram, $option;

        $db = & JFactory::getDBO();
        $uri = & JFactory::getURI();
        $user = & JFactory::getUser();
        $model = & $this->getModel();
        $sectionid = 0;
        $items = & $this->get('Data');
       
        $cm_items = & $this->get('CommentData');
        $isNew = !isset($items->id) || ($items->id < 1);

        if (!$isNew) {
            $sectionid = $items->sectionid;
            $model->checkout($user->get('id'));
        } else {
            $items->title = "";
            $items->introtext = "";
            $items->alias = "";
            $items->fulltext = "";
            $items->assigned_user = $user->get('id');
            $items->state = 1;
            $items->catid = JRequest::getVar('catid', 0, 'post', 'int');
            $items->sectionid = JRequest::getVar('sectionid', 0, 'post', 'int');
        }

        $javascript = "onchange=\"changeDynaList( 'catid', sectioncategories, document.adminForm.sectionid.options[document.adminForm.sectionid.selectedIndex].value, 0, 0);\"";



        $list = array();
        //$lists['state'] = JHTML::_('grid.state', $filter_state, "Published", "UnPublished");
        $lists['state'] = JHTML::_('select.booleanlist', 'state', 'class="inputbox"', $items->state);
        //select category
        // $lists['catid'] = JHTML::_('list.category', 'catid', $option, intval($items->catid));
        $lists['sectionid'] = JHTML::_('list.section', 'sectionid', intval($items->sectionid));




        $sections = ArticleflowHelper::buildSection();
        $lists['sectionid2'] = JHTML::_('select.genericlist', $sections, 'sectionid', 'class="inputbox" size="1" ' . $javascript, 'id', 'title', intval($items->sectionid));

        foreach ($sections as $section) {
            $section_list[] = (int) $section->id;
            // get the type name - which is a special category
            if ($items->sectionid) {
                if ($section->id == $items->sectionid) {
                    $contentSection = $section->title;
                }
            } else {
                if ($section->id == $sectionid) {
                    $contentSection = $section->title;
                }
            }
        }

        ///Begin

        $sectioncategories = array();
        $sectioncategories[-1] = array();
        $sectioncategories[-1][] = JHTML::_('select.option', '-1', JText::_('Select Category'), 'id', 'title');
        $section_list = implode('\', \'', $section_list);

        $query = 'SELECT id, title, section' .
                ' FROM #__categories' .
                ' WHERE section IN ( \'' . $section_list . '\' )'
        ;
        $db->setQuery($query);
        $cat_list = $db->loadObjectList();

        // Uncategorized category mapped to uncategorized section   `1
        $uncat = new stdClass();
        $uncat->id = 0;
        $uncat->title = JText::_('Uncategorized');
        $uncat->section = 0;
        $cat_list[] = $uncat;
        foreach ($sections as $section) {
            $sectioncategories[$section->id] = array();
            $rows2 = array();
            foreach ($cat_list as $cat) {
                if ($cat->section == $section->id) {
                    $rows2[] = $cat;
                }
            }
            foreach ($rows2 as $row2) {
                $sectioncategories[$section->id][] = JHTML::_('select.option', $row2->id, $row2->title, 'id', 'title');
            }
        }
        $sectioncategories['-1'][] = JHTML::_('select.option', '-1', JText::_('Select Category'), 'id', 'title');
        $categories = array();
        foreach ($cat_list as $cat) {
            if ($cat->section == $items->sectionid)
                $categories[] = $cat;
        }

        $categories[] = JHTML::_('select.option', '-1', JText::_('Select Category'), 'id', 'title');
        $lists['catid'] = JHTML::_('select.genericlist', $categories, 'catid', 'class="inputbox" size="1"', 'id', 'title', intval($items->catid));


        ///       End
        // build the html select list for ordering
        $this->assignRef('cm_items', $cm_items);
        $this->assignRef('lists', $lists);
        $this->assignRef('items', $items);
        $this->assignRef('sectioncategories', $sectioncategories);
        parent::display($tpl);
    }

}

?>
